package com.lyc.controller;

import com.lyc.constant.Constant;
import com.lyc.entity.Color;
import com.lyc.entity.vo.PageBean;
import com.lyc.service.IColorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/color")
public class ColorController {

    @Autowired
    private IColorService colorService;

    @RequestMapping("/colorList")
    public String colorList(Integer currentPage, Model model){
        if (currentPage==null||currentPage<=0)
            currentPage=1;
        PageBean<Color> page = colorService.selectByPage(currentPage, Constant.COLOR_PAGE_SIZE);
        model.addAttribute("page",page);
        return "admin/color_list";
    }

    @RequestMapping("/toAddOrUpdateColorPage")
    public String addOrUpdateColor(Integer colorId, Model model){
        if (colorId!=null&&colorId>0){
            Color color = colorService.selectByColorId(colorId);
            model.addAttribute("color",color);
        }
        return "admin/color_add";
    }

    @RequestMapping("/addOrUpdateColor")
    public String addOrUpdateColor(Color color){
        if (color == null || color.getColorId() == null || color.getColorId() <= 0) {
            //添加操作
            colorService.addColor(color);
        }else {
            //修改操作
            colorService.updateColor(color);
        }
        return "redirect:/color/colorList";
    }

    @RequestMapping("/deleteColor")
    public String deleteColor(Integer colorId){
        if (colorId!=null) {
            //添加操作
            colorService.deleteColorByColorId(colorId);
        }
        return "redirect:/color/colorList";
    }
}
